package com.city.justlisten.controller;


import com.city.justlisten.entity.Song;
import com.city.justlisten.service.SongService;
import com.city.justlisten.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 歌曲管理controller
 */
@RestController
@RequestMapping("/song")
public class SongController {

    @Autowired
    private SongService songService;

    /**
     * 添加歌曲
     */
    @PostMapping("/add")
    public Result addSong(HttpServletRequest request, @RequestParam("file") MultipartFile mpFile){
        String singerId = request.getParameter("singerId").trim();
        String name = request.getParameter("name").trim();
        String introduction = request.getParameter("introduction").trim();
        String pic = "/img/songPic/tubiao.jpg";
        String lyric = request.getParameter("lyric").trim();
        //上传歌曲文件
        if(mpFile.isEmpty()){
            return Result.error();
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+mpFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"song";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeUrlPath = "/song/"+fileName;
        try {
            mpFile.transferTo(dest);
            Song song = new Song();
            song.setSingerId(Integer.parseInt(singerId));
            song.setName(name);
            song.setIntroduction(introduction);
            song.setPic(pic);
            song.setLyric(lyric);
            song.setUrl(storeUrlPath);
            boolean flag = songService.insert(song);
            if(flag){
                return Result.ok().data("avator",storeUrlPath);
            }
            return Result.error();
        } catch (IOException e) {
            return Result.error();
        }
    }

    /**
     * 根据歌手id查询歌曲
     */
    @GetMapping("/singer/detail")
    public Result songOfSingerId(@RequestParam("singerId")String singerId){
        List<Song> songs = songService.songOfSingerId(Integer.parseInt(singerId));
        return Result.ok().data("songs",songs);
    }

    /**
     * 修改歌曲
     */
    @PostMapping("/update")
    public Result updateSong(HttpServletRequest request){
        String id = request.getParameter("id").trim();
        String name = request.getParameter("name").trim();
        String introduction = request.getParameter("introduction").trim();
        String lyric = request.getParameter("lyric").trim();

        //保存到歌手的对象中
        Song song = new Song();
        song.setId(Integer.parseInt(id));
        song.setName(name);
        song.setIntroduction(introduction);
        song.setLyric(lyric);
        boolean flag = songService.update(song);
        if(flag){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 删除歌曲
     */
    @GetMapping("/delete")
    public Result deleteSinger(@RequestParam("id")String id){
        boolean flag = songService.delete(Integer.parseInt(id));
        if (flag){
            return Result.ok();
        }
        return Result.error();
    }

    /**
     * 更新歌曲图片
     */
    @PostMapping("/updateSongPic")
    public Result updateSongPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        if(avatorFile.isEmpty()){
            return Result.error();
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"
                +System.getProperty("file.separator")+"songPic";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/img/songPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Song song = new Song();
            song.setId(id);
            song.setPic(storeAvatorPath);
            boolean flag = songService.update(song);
            if(flag){
                return Result.ok().data("pic",storeAvatorPath);
            }
            return Result.error();
        } catch (IOException e) {
            return Result.error();
        }
    }


    /**
     * 更新歌曲
     */
    @PostMapping("/updateSongUrl")
    public Result updateSongUrl(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        if(avatorFile.isEmpty()){
            return Result.error();
        }
        //文件名=当前时间到毫秒+原来的文件名
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        //文件路径
        String filePath = System.getProperty("user.dir")+System.getProperty("file.separator")+"song";
        //如果文件路径不存在，新增该路径
        File file1 = new File(filePath);
        if(!file1.exists()){
            file1.mkdir();
        }
        //实际的文件地址
        File dest = new File(filePath+System.getProperty("file.separator")+fileName);
        //存储到数据库里的相对文件地址
        String storeAvatorPath = "/song/"+fileName;
        try {
            avatorFile.transferTo(dest);
            Song song = new Song();
            song.setId(id);
            song.setUrl(storeAvatorPath);
            boolean flag = songService.update(song);
            if(flag){
                return Result.ok().data("avator",storeAvatorPath);
            }
            return Result.error();
        } catch (IOException e) {
            Result.error();
        }
        return Result.ok();
    }

    /**
     * 根据歌曲id查询歌曲对象
     */
    @GetMapping("/detail")
    public Result detail(@RequestParam("songId")String songId){
        Song song = songService.selectByPrimaryKey(Integer.parseInt(songId));
        return Result.ok().data("song",song);
    }

    /**
     * 根据歌手名字精确查询歌曲
     */
    @GetMapping("/songOfSongName")
    public Result songOfSongName(@RequestParam("songName")String songName){
        List<Song> songs = songService.songOfName(songName);
        return Result.ok().data("song",songs);
    }

    /**
     * 根据歌手名字模糊查询歌曲
     */
    @GetMapping("/likeSongOfName")
    public Result likeSongOfName(@RequestParam("songName")String songName){
        List<Song> songs = songService.likeSongOfName(songName);
        return Result.ok().data("song",songs);
    }

    /**
     * 查询所有歌曲
     */
    @GetMapping("/allSong")
    public Result allSong(){
        List<Song> songs = songService.allSong();
        return Result.ok().data("song",songs);
    }

}




















